<?php

declare(strict_types=1);

namespace app\middleware;

use app\api\service\DmjUserService;

class ApiUserAuth
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     *
     * @return Response
     */
    public function handle($request, \Closure $next, $continue = '')
    {
        $token = $request->header('Authorization', '');
        if (!$token) {
            if ($request->has('anfen_debug')) {
                $token = $request->param('anfen_debug');
            }
        }
        if ($token) {
            $request->user = invoke(DmjUserService::class)->oilUserCache($token);
        } else {
            $request->user = [];
        }

        if (!$request->user && !$continue) {
            return fail('请先登录', 401);
        }

        return $next($request);

        /*  if (!session('?dmjuser')) {
             if ($continue) {
                 $request->user = [];
             } else {
                 return fail('请先登录', 401);
             }
         } else {
             $request->user = session('dmjuser');
         } */

        return $next($request);
    }
}
